package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Scene;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class DeleteSceneActivity extends FragmentActivity implements LoaderCallbacks<Cursor>, OnClickListener{
	private static final String TAG="DeleteSceneActivity";
	
	private ListView mListScenes;
	private Button mbtnDeleteAll;
	private Button mbtnDeleteSelected;
	
	private SceneAdapter mSceneAdapter;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_delete_scene);
		init_view();
		//load data
		getSupportLoaderManager().initLoader(0, null, this);
	}
	
	private void init_view(){
		mListScenes=(ListView)findViewById(R.id.activity_delete_scene_list_scene);
		mbtnDeleteAll=(Button)findViewById(R.id.activity_delete_scene_delete_all);
		mbtnDeleteSelected=(Button)findViewById(R.id.activity_delete_scene_delete_selected);
		
		mbtnDeleteAll.setOnClickListener(this);
		mbtnDeleteSelected.setOnClickListener(this);
		
		mSceneAdapter=new SceneAdapter(getApplicationContext(), null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		mListScenes.setAdapter(mSceneAdapter);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(getApplicationContext(), TBL_Scene.CONTENT_URI_SCENE, null, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		mSceneAdapter.swapCursor(arg1);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mSceneAdapter.swapCursor(null);
	}
	
	private static class SceneAdapter extends CursorAdapter{
		private int[] mListIDScene;
		public SceneAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
			mListIDScene=new int[c.getCount()];
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView txtName=(TextView)view.findViewById(R.id.item_list_delete_scene_txt_name);
			CheckBox chbDelete=(CheckBox)view.findViewById(R.id.item_list_delete_scene_chb_delete);
			int position=cursor.getPosition();
			
			chbDelete.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
				}
			});
			String name=cursor.getString(cursor.getColumnIndex(TBL_Scene.NAME));
			txtName.setText(name);
		}

		@Override
		public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
			return View.inflate(arg0, R.layout.item_list_delete_scene, null);
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_delete_scene_delete_all:
			
			break;
		case R.id.activity_delete_scene_delete_selected:
			
			break;

		default:
			break;
		}
	}
	
}